package com.rongc.client.freight.business.supply.view.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.rongc.client.core.gson.GsonHelper;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.base.view.widget.BaseInputDialog;
import com.rongc.client.freight.business.carrier.model.ConveyanceBean;
import com.rongc.client.freight.business.carrier.model.ConveyanceList;
import com.rongc.client.freight.business.carrier.request.api.ConveyanceFindApi;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.request.api.SupplyCancelApi;
import com.rongc.client.freight.business.supply.request.api.SupplyModifyPriceApi;
import com.rongc.client.freight.business.supply.view.fragment.SupplyConveyanceFragment;
import com.rongc.client.freight.business.supply.view.fragment.SupplyFragment;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.flyco.dialog.widget.NormalDialog.STYLE_TWO;

/**
 * Created by Bruce on 2017/7/7.
 */
public class SupplyWaybillActivity extends BaseActivity implements View.OnClickListener{
    SupplyConveyanceFragment fragment;
    String goodsid;
    SupplyBean supplyBean;
    List<ConveyanceBean> conveyanceBeanList;

    SupplyFragment supplyFragment;

    NormalDialog dialog;
    BaseInputDialog inputDialog;
    int page = 1;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_jia:
                inputDialog.show();
                break;
            case R.id.tv_status:
                dialog.show();
                break;
        }
    }

    Response.Listener<JSONObject> respWorkersListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                try {
                    supplyBean = GsonHelper.getDeserializer().fromJson(response.getString("goodInfo"), SupplyBean.class);
                    ConveyanceList atemp = GsonHelper.getDeserializer().fromJson(response.getString("driverInfos"), ConveyanceList.class);
                    conveyanceBeanList.clear();
                    if(atemp != null && atemp.getList() != null)
                        conveyanceBeanList.addAll(atemp.getList());

                    fragment.setDatas(conveyanceBeanList,page);
                    fragment.setPrice(supplyBean.getFreightByType());

                    supplyFragment.setData(supplyBean);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                UtilMethod.dismissProgressDialog(SupplyWaybillActivity.this);
            }
        }
    };

    Response.Listener<NullResult> respWaybillListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                dialog.cancel();
                finish();
            } else {
                UtilMethod.dismissProgressDialog(SupplyWaybillActivity.this);
            }
        }
    };

    Response.Listener<String> respPriceListener = new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            if (HandleCode.requestSuccess()) {
                supplyBean.setFreight(response);
                supplyFragment.setData(supplyBean);

                fragment.setPrice(response);
                inputDialog.cancel();
            } else {
                UtilMethod.dismissProgressDialog(SupplyWaybillActivity.this);
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(SupplyWaybillActivity.this);
        }
    };

    @Override
    public void callbackFun1(Bundle arg) {
        page = arg.getInt("page",page);
        doFind(page);
    }

    void doFind(int page){
        this.page = page;

        ConveyanceFindApi p = new ConveyanceFindApi(new ConveyanceFindApi.SupplyFindDriverParams(
                goodsid,page+""),
                respWorkersListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public void initData() {
        doFind(1);
    }

    @Override
    public void initParams() {
        goodsid = getIntent().getStringExtra("data");
    }

    @Override
    public void initView() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

        initToolbar(R.string.supply_waybill_title);
        conveyanceBeanList = new ArrayList<>();
        fragment = SupplyConveyanceFragment.getInstance(goodsid);
        ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),fragment,R.id.contentFrame);
        supplyFragment = (SupplyFragment) getSupportFragmentManager().findFragmentByTag("supply");
        supplyFragment.setListener(this);

        makeDialog();
        makeInputDialog();
    }

    void makeDialog(){
        dialog = new NormalDialog(this);
        dialog.content("您找到车子了吗，要不再等等？")
                .style(STYLE_TWO)
                .btnText(new String[]{"取消","确定"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.cancel();
                    }
                },new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        //  取消绑定
                        SupplyCancelApi p = new SupplyCancelApi(new SupplyCancelApi.SupplyCancelParams(
                                goodsid),
                                respWaybillListener, errorListener);
                        RequestManager.getInstance().call(p);

                    }
                });
    }

    void makeInputDialog(){
        inputDialog = new BaseInputDialog(this);
        inputDialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                inputDialog.cancel();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                SupplyModifyPriceApi p = new SupplyModifyPriceApi(new SupplyModifyPriceApi.SupplyModifyPriceParams(
                        goodsid, inputDialog.getPrice()),
                        respPriceListener, errorListener);
                RequestManager.getInstance().call(p);
            }
        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_supply_waybill;
    }
}
